/**
  * This file is part of Medusa.
  *
  * Authors: Uriel Fanelli <uriel.fanelli@gmail.com>
  *          Perthect <perthect@gmail.com>
  *          Marco Bizzarri <Marco.Bizzarri@gmail.com>
  *
  *
  * Medusa is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation; either version 2 of the License, or
  * (at your option) any later version.

  * Medusa is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.

  * You should have received a copy of the GNU General Public License
  * along with Medusa; if not, write to the Free Software
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  *
  */


#include "proxy_settings.h"
#include "goongress.h"

handleProxyConfig::handleProxyConfig()
{
leggiProxyConfig();
}


handleProxyConfig::~handleProxyConfig()
{
 salvaProxyConfig();

}


void handleProxyConfig::apply_settings()
{
    this->salvaProxyConfig();
}

void handleProxyConfig::salvaProxyConfig()
{
   // Crea un'istanza + file di configurazione in formato xml (1)
   QSettings proxy_config(CONFIG_FORMAT, QSettings::UserScope,COMPANY_NAME, APPLICATION_NAME);
   proxy_config.beginGroup("Proxy_Configuration");
   proxy_config.setValue("proxyHost", this->proxy_host);
   proxy_config.setValue("proxyPort", this->proxy_port);
   proxy_config.setValue("proxyUser", this->proxy_user);
   proxy_config.setValue("proxyPass", this->proxy_pass);
   proxy_config.setValue("proxyType", this->proxy_type);
   proxy_config.endGroup();
}

void handleProxyConfig::leggiProxyConfig()
{
 // Legge un'istanza + file di configurazione in formato xml (1)
 QSettings proxy_config(CONFIG_FORMAT, QSettings::UserScope,COMPANY_NAME, APPLICATION_NAME);
 proxy_config.beginGroup("Proxy_Configuration");
 this->proxy_host=QString(proxy_config.value("proxyHost","").toString());
 this->proxy_port=QString(proxy_config.value("proxyPort","").toString());
 this->proxy_user=QString(proxy_config.value("proxyUser","").toString());
 this->proxy_pass=QString(proxy_config.value("proxyPass","").toString());
 this->proxy_type=QString(proxy_config.value("proxyType","").toString());
 proxy_config.endGroup();
}

void handleProxyConfig::deployProxy()
{
    QNetworkProxy proxy;
    proxy.setType(QNetworkProxy::NoProxy);
    proxy.setHostName(proxy_host);
    proxy.setPort(proxy_port.toInt());
    proxy.setUser(proxy_user);
    proxy.setPassword(proxy_pass);

    if (proxy_type.contains("http"))
    {
      proxy.setType(QNetworkProxy::HttpProxy);
    }
     if (proxy_type.contains("socks"))
    {
      proxy.setType(QNetworkProxy::Socks5Proxy);
    }


  QNetworkProxy::setApplicationProxy(proxy);
}



